package Day23;

import java.io.*;
import java.net.Socket;

public class Demo01 {
    //快递柜网络编程
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //客户端
        Socket socket = new Socket("localhost", 65535);
        View v = new View();
        DoExpress d = new DoExpress();
        Express e = new Express();
        bb:while (true) {
            int a = v.xxx();
            aa:switch (a) {
                case 1: {
                    int b = v.courier();
                    switch (b) {
                        case 1: {
                            //快递录入
                            printView(socket,"add");
                            inputView(socket);
                            boolean flag = d.add(v.add());
                            if (flag == true) {
                                v.success();
                            } else {
                                v.fail();
                            }
                            break;
                        }
                        case 2: {
                            //快递删除
                            printView(socket,"delete");
                            inputView(socket);
                            OutputStream os = socket.getOutputStream();
                            ObjectOutputStream oos = new ObjectOutputStream(os);
                            oos.writeObject("delete");
                            boolean flag = d.delete(v.delete());
                            if (flag == true) {
                                v.success();
                            } else {
                                v.fail();
                            }
                            break;
                        }
                        case 3: {
                            //快递修改
                            printView(socket,"revise");
                            inputView(socket);
                            boolean flag = d.revise(v.revise());
                            if (flag == true) {
                                v.success();
                            } else {
                                v.fail();
                            }
                            break;
                        }
                        case 4: {
                            printView(socket,"printAll");
                            inputView(socket);
                            try {
                                d.printfAll();
                            } catch (ClassNotFoundException classNotFoundException) {
                                classNotFoundException.printStackTrace();
                            }
                            break;
                        }
                        case 0: {
                            break aa;
                        }
                    }
                    break;
                }
                case 2:{
                    //取快递
                    printView(socket,"user");
                    inputView(socket);
                    boolean flag = d.user(v.user());
                    if (flag == true) {
                        v.success();
                    } else {
                        v.fail();
                    }
                    break;
                }
                case 0:{
                    break bb;
                }
            }
        }
    }
    private static void printView(Socket socket,String s) throws IOException {
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(s);
//        oos.close();
//        os.close();
    }

    private static void inputView(Socket socket) throws IOException, ClassNotFoundException {
        InputStream is = socket.getInputStream();
        ObjectInputStream ois = new ObjectInputStream(is);
        String s = (String) ois.readObject();
        System.out.println(s);
//        is.close();
//        ois.close();
    }
}
